/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.helper.EncCommonHelper;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class EstBusinessCountExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"EstBusinessCount.xls\"");
		HSSFFont font = workbook.createFont();
		font.setFontHeightInPoints(10);
		font.setColor(10);
		font.setFontName("黑体");
		font.setBoldweight(700);
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setFont(font);
		cellStyle.setAlignment(2);
		cellStyle.setBorderBottom(1);
		cellStyle.setWrapText(true);
		cellStyle.setFillForegroundColor(22);
		cellStyle.setFillPattern(1);
		HSSFSheet sheet = workbook.createSheet("list");
		HSSFRow r = sheet.createRow(0);
		HSSFCell c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		r.createCell(9);
		r.createCell(10);
		c.setCellValue(new HSSFRichTextString("合同金额：" + model.get("costTotal")
				+ "（已优惠：" + model.get("favTotal") + "元） 调账："
				+ model.get("adjTotal") + "元 已收：" + model.get("recTotal")
				+ "元 欠款：" + model.get("remTotal") + "元"));
		sheet.addMergedRegion(new Region(0, 0, 0, 10));
		r = sheet.createRow(1);
		r.createCell(0).setCellValue(new HSSFRichTextString("类别"));
		r.createCell(1).setCellValue(new HSSFRichTextString("等级"));
		r.createCell(2).setCellValue(new HSSFRichTextString("地区"));
		r.createCell(3).setCellValue(new HSSFRichTextString("名称"));
		r.createCell(4).setCellValue(new HSSFRichTextString("联系人"));
		r.createCell(5).setCellValue(new HSSFRichTextString("电话"));
		r.createCell(6).setCellValue(new HSSFRichTextString("经办人"));
		r.createCell(7).setCellValue(new HSSFRichTextString("合同金额（元）"));
		r.createCell(8).setCellValue(new HSSFRichTextString("调账（元）"));
		r.createCell(9).setCellValue(new HSSFRichTextString("已收（元）"));
		r.createCell(10).setCellValue(new HSSFRichTextString("欠款（元）"));
		List list = (List) model.get("list");
		List etList = (List) model.get("estTypeList");
		List elList = (List) model.get("estLevelList");
		List areaList = (List) model.get("areaList");

		for (int os = 0; os < list.size(); ++os) {
			Object[] objs = (Object[]) list.get(os);
			r = sheet.createRow(os + 2);
			String linkman = "";
			String linkmanPhone = "";
			String managerName = "";
			if (objs[10] != null) {
				linkman = objs[10].toString();
			}

			if (objs[11] != null) {
				linkmanPhone = objs[11].toString();
			}

			if (objs[9] != null) {
				managerName = objs[9].toString();
			}

			BigDecimal cost = new BigDecimal(objs[0].toString());
			BigDecimal adjCost = new BigDecimal(objs[2].toString());
			BigDecimal recCost = new BigDecimal(objs[3].toString());
			BigDecimal remCost = cost.add(adjCost).subtract(recCost);
			r.createCell(0).setCellValue(
					new HSSFRichTextString(EncCommonHelper.getSimpleObjectName(
							etList, Long.parseLong(objs[7].toString()))));
			r.createCell(1).setCellValue(
					new HSSFRichTextString(EncCommonHelper.getSimpleObjectName(
							elList, Long.parseLong(objs[8].toString()))));
			r.createCell(2).setCellValue(
					new HSSFRichTextString(EncCommonHelper.getSimpleObjectName(
							areaList, Long.parseLong(objs[6].toString()))));
			r.createCell(3).setCellValue(
					new HSSFRichTextString(objs[5].toString()));
			r.createCell(4).setCellValue(new HSSFRichTextString(linkman));
			r.createCell(5).setCellValue(new HSSFRichTextString(linkmanPhone));
			r.createCell(6).setCellValue(new HSSFRichTextString(managerName));
			r.createCell(7).setCellValue(cost.doubleValue());
			r.createCell(8).setCellValue(adjCost.doubleValue());
			r.createCell(9).setCellValue(recCost.doubleValue());
			r.createCell(10).setCellValue(remCost.doubleValue());
		}

		ServletOutputStream arg22 = response.getOutputStream();
		workbook.write(arg22);
		arg22.flush();
		arg22.close();
	}
}